<?php

class Razdel {
    public static function Add(){
        $form = NULL;
        $request = getRequest::getInstance();
				$id = $request->getVar('id', 'GET');
				$pag_type = NULL;
				if($id==0){
					$uroven = 1;
					$razdel = "0";
				}else{
					$find = ORM::factory('razdel', $id)->Select('uroven, type')->find_all();
					$uroven = $find[0]['uroven'];
					$pag_type = $find[0]['type'];
					unset($find);
					$uroven = $uroven+1;
					$razdel = $id;
				}
                $post = $request->postParams();
				if($request->isPost()==true){
					$pag_type = $post['type'];
					if(!empty($post['title']) && !empty($post['url'])){
						$orm = ORM::factory('razdel');
											
						$default="0";
						if(isset($post['default'])){
							   $default = Razdel::Operations('default', array('uroven'=>$post['uroven']));
						}

					   $onmenu = "0";
					   if(isset($post['onmenu'])){
							$onmenu = $post['onmenu'];
					   }
					   $published = "0";
					   if(!empty($post['published'])){
							$published = $post['published'];
					   }
					$orm->title = $post['title'];
					$orm->url = $post['url'];
					$orm->uroven = $post['uroven'];
						
						if($post['razdel']=="0"){
							$p_razdel = NULL;
						}else{
							$p_razdel = $post['razdel'];
						}
						$orm->razdel = $p_razdel;
						$orm->sort = $post['order'];
						$orm->index = $default;
						$orm->published	= $published;
						$orm->keywords = $post['keywords'];
						$orm->description = $post['description'];
						$orm->type = $post['type'];
						$orm->onmenu = $onmenu;
						$orm->flush();

						$id_razdel = $orm->return_id();

						unset($orm);

						if(!empty($id_razdel)){
							$orm = ORM::factory('content');
							
							$orm->razdel = $id_razdel;

								$content = $request->getVar('content', 'POST',1);
								$descrip = $request->getVar('descrip', 'POST',1);						
						if(empty($content)){$body = "<p></p>";}else{$body =& $content;}
							$orm->body = htmlspecialchars($body, ENT_QUOTES, 'UTF-8');
						if(empty($descrip)){$descrip = "<p></p>";}else{$descrip =& $descrip;}
							$orm->descrip = htmlspecialchars($descrip, ENT_QUOTES, 'UTF-8');

							$orm->flush();
							unset($orm);
						}
					}

					if(empty($post['uroven']) || $post['uroven'] == 0 || empty($post['razdel'])){
						header('location: /admin.html');
					}else{
						header('location: /admin/razdel.html?id='.$post['razdel']);
					}

				}
          return array('uroven'=>$uroven, 'razdel'=>$razdel, 'pag_type'=>$pag_type,'id'=>$id);
    }
    public static function Edit(){
        $request = getRequest::getInstance();
        $id = $request->getVar('id', 'GET');
		$edit_id = $request->getVar('edit_id', 'POST');
        
        if(!empty($edit_id)){
            $body = $request->getVar('content', 'POST',1);
			$body = htmlspecialchars($body, ENT_QUOTES, 'UTF-8');
            $descrip = $request->getVar('descrip', 'POST',1);
			$descrip = htmlspecialchars($descrip, ENT_QUOTES, 'UTF-8');
            $title = $request->getVar('title', 'POST');

            ORM::factory()->prepare("UPDATE `razdel` SET razdel.title='".$title."' WHERE razdel.id='".$edit_id."'")->execute();
            ORM::factory()->prepare("UPDATE `content` SET content.body=\"".$body."\", content.descrip=\"".$descrip."\" WHERE content.razdel='".$edit_id."'")->execute();
            #ORM::factory()->prepare("UPDATE `content` SET content.body='".htmlspecialchars($body)."', content.descrip='".htmlspecialchars($descrip)."' WHERE content.razdel='".$edit_id."'")->execute();

            $orm = ORM::factory('razdel', $edit_id);

            if($orm->uroven == 1){
                    header('location: /admin.html');
            }else{
                    header('location: /admin/razdel.html?id='.$orm->razdel);
            }
        }else{
			//echo"ID: ".$id."<br>";
			//echo "SELECT razdel.id, razdel.title, content.razdel, content.body, content.descrip FROM `razdel`,`content` WHERE razdel.id='".$id."' AND content.razdel = razdel.id<br>";
			$result = ORM::factory()->prepare("SELECT razdel.id, razdel.title, content.razdel, content.body, content.descrip FROM `razdel`,`content` WHERE razdel.id='".$id."' AND content.razdel = razdel.id")->getResult();
            //$result = ORM::factory()->Where(array("razdel.id" => $id, "content.razdel" => "razdel.id"))->Select("razdel.id, razdel.title, content.razdel, content.body, content.descrip")->crossjoin('razdel, content')->find_all();

            return array('title'=>$result[0]['title'],'edit'=>$result[0],'id'=>$id);
		}
    }
    public static function Setup(){
        $request = getRequest::getInstance();
	$id = $request->getVar('id', 'GET');
	$edit_id = $request->getVar('edit_id', 'POST');
        $result = array();
				if(!empty($edit_id)){
						$post = $request->postParams();

						$orm = ORM::factory('razdel', $edit_id);
						$uroven = $orm->uroven;
						$razdel = $orm->razdel;

						if(isset($post['default'])){
                                                                $default = Razdel::Operations('default', array('uroven'=>$uroven));
                                                }else{
							$default="0";
						}
                                                $onmenu = "0";
                                                if(isset($post['onmenu'])){
                                                  $onmenu = $post['onmenu'];
                                                }

                                                ORM::factory('razdel')->prepare("UPDATE `razdel` SET `title`='".$post['title']."', `url`='".$post['url']."', `sort`='".$post['order']."', `index`='".$default."', `published`='".$post['published']."', `keywords`='".$post['keywords']."', `description`='".$post['description']."', `type`='".$post['type']."', `onmenu`='".$onmenu."' WHERE `id`='".$edit_id."'")->execute();


						if($uroven==1 && $razdel==0){
							header('location: /admin.html');
						}else{
							header('location: /admin/razdel.html?id='.$razdel);
						}
				}else{
					$result = ORM::factory('razdel')->Where("id", $id)->find_all();					
				}
        return array('title'=>$result[0]['title'],'edit'=>$result[0],'id'=>$id);
    }

    public static function Operations($type=NULL, $params=NULL, $id=NULL){
			if(!empty($id) && $type=='del'){
				ORM::factory('razdel', $id)->Delete();
                  ORM::factory('razdel')->Where("id", $id)->Delete();
			}elseif(!empty($id) && $type=='pub'){
				ORM::factory()->prepare("UPDATE `razdel` SET razdel.published='1' WHERE razdel.id='".$id."'")->execute();
			}elseif(!empty($id) && $type=='unpub'){
				ORM::factory()->prepare("UPDATE `razdel` SET razdel.published='0' WHERE razdel.id='".$id."'")->execute();
			}elseif(!empty($id) && $type=='activ'){
				ORM::factory()->prepare("UPDATE `users` SET `activ`='1' WHERE `id`='".$id."'")->execute();
			}elseif(!empty($id) && $type=='unactiv'){
				ORM::factory()->prepare("UPDATE `users` SET `activ`='0' WHERE `id`='".$id."'")->execute();
			}elseif($type == "default"){
				$default="1";
				$id_r = ORM::factory('razdel')->Where(array("index" => 1, "uroven" => 1))->id;

				if(!empty($id_r)){
					if(isset($params['uroven'])){
						$uroven = $params['uroven'];
					}else{
						$uroven = 1;
					}
					if($uroven==1){
						ORM::factory()->prepare("UPDATE `razdel` SET razdel.index='0' WHERE razdel.id='".$id_r."'")->execute();
					}else{
						$default="0";
					}
				}else{
					$default="1";
				}
				if(!empty($id)){
					ORM::factory()->prepare("UPDATE `razdel` SET razdel.index='1' WHERE razdel.id='".$id."'")->execute();
				}
				return $default;
			}elseif($type == "order"){
				$count = getRequest::getInstance()->getVar('int','GET');
				if(empty($count) || $count==0){
					$sort = "razdel.sort=razdel.sort+1";
				}else{
					$sort = "razdel.sort='".$count."'";
				}
				ORM::factory()->prepare("UPDATE `razdel` SET ".$sort." WHERE razdel.id='".$id."'")->execute();
			}elseif($type == "top"){
				$count = getRequest::getInstance()->getVar('int','GET');
				if(empty($count) || $count==0){
					$sort = "goods.sort=goods.sort+1";
				}else{
					$sort = "goods.sort='".$count."'";
				}
				ORM::factory()->prepare("UPDATE `goods` SET ".$sort." WHERE goods.id='".$id."'")->execute();
			}
    }
}
?>
